package com.brave.shop.service;

import com.brave.base.BaseService;
import com.brave.shop.dto.FindShopGoodsByTypeDTO;
import com.brave.shop.entity.MCShopDictTypeEntity;
import com.brave.shop.entity.MCShopGoodsDetailEntity;
import com.brave.shop.entity.MCShopGoodsEntity;
import com.brave.shop.repository.MCShopDictTypeRepository;
import com.brave.shop.repository.MCShopGoodsDetailRepository;
import com.brave.shop.repository.MCShopGoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ProjectName: has-free-server-api
 * @PackageName: com.brave.shop.service
 * @UserName: BraveKing-wy
 * @DateTime: 2019/10/7 20:52
 * @Description:
 **/
@Service
public class ShopQueryService extends BaseService
{
    @Autowired
    private MCShopDictTypeRepository shopDictTypeRepository;
    @Autowired
    private MCShopGoodsRepository shopGoodsRepository;
    @Autowired
    private MCShopGoodsDetailRepository shopGoodsDetailRepository;
    
    
    /**
     * 2019年10月7日 20:53:26
     * 查询商城商品类型列表
     * @return
     */
    public String findShopTypeList()
    {
        List<MCShopDictTypeEntity> dictList = shopDictTypeRepository.findAll();
        return success(dictList);
    }


    /**
     * 2019年10月7日 20:57:22
     * 查询商城类型下的商品列表
     * @param dto
     * @return
     */
    public String findShopGoodsByType(FindShopGoodsByTypeDTO dto)
    {
        StringBuilder querySQL = new StringBuilder();
        querySQL.append("SELECT * FROM mc_shop_goods g WHERE goods_type =");
        querySQL.append(dto.getTypeId());
        Map<String, Object> resultMap = shopGoodsRepository.findPageList(querySQL.toString(), dto.getPageSize(), dto.getPageNumber());
        return success(resultMap);
    }


    /**
     * 2019年10月8日 10:39:25
     * 查询商品的详细信息
     * @param goodsId
     * @return
     */
    public String findShopGoodsDetailInfo(long goodsId)
    {
        Optional<MCShopGoodsEntity> goodsOptional = shopGoodsRepository.findById(goodsId);
        if(goodsOptional.isPresent())
        {
            //结果
            Map<String, Object> resultMap = new HashMap<>(4);
            resultMap.put("goodsInfo", goodsOptional.get());
            //详细信息
            MCShopGoodsDetailEntity detailInfo = shopGoodsDetailRepository.findFirstByGoodsId(goodsId);
            resultMap.put("goodsDetailInfo", detailInfo);
            return success(resultMap);
        }
        return success();
    }
    
    
}